package controllers;

import java.util.ArrayList;
import java.util.List;

import play.mvc.Controller;

import models.Entities;
import models.Entities;
import models.Idea;
import models.Idea;
import models.Relation;
import models.Relation;
import models.Tag;

/**
 * This class is responsible for the quick search for Entites, Ideas, Relations,
 * and Tags
 * 
 * @author Ahmed Ammar
 */
public class QuickSearch extends Controller {
	/**
	 * QSearchResults is a method that should give the results of the search, we
	 * create a list for each of the Entites, Tags, Ideas, and Relations that
	 * should contain the names that matches the search. Last thing are for
	 * loops for each of them, each loop adds to the search results the Entites,
	 * Tags, Ideas, or Relations that was contained in the lists and then
	 * rendering the ArrayList of results to display the Total Results.
	 * 
	 * @param String
	 *            search, the string to be searched
	 * @author Ahmed Ammar
	 */
	public static void quickSearchResults(String search) {

		List<Entities> Eres = Entities.search(search);
		List<Tag> Tres = Tag.search(search);
		List<Idea> Ires = Idea.search(search);
		List<Relation> Rres = Relation.search(search);

		int length = Eres.size() + Tres.size() + Ires.size() + Rres.size();

		render(Eres, Tres, Ires, Rres, length, search);
	}

	/**
	 * exportToCSV is a method that helps exports the search results to CSV
	 * format, it takes the search string and then get lists of entities, tags,
	 * ideas, relations, then the HTTP sets the text into CSV format, and then
	 * name the file that should be exported, and finally rendering the .csv
	 * file along with the lists of contents.
	 * 
	 * @param search
	 * @author Ahmed Ammar
	 */
	public static void exportToCSV(String search) {

		List<Entities> entityResult = Entities.search(search);
		List<Tag> tagResult = Tag.search(search);
		List<Idea> ideaResult = Idea.search(search);
		List<Relation> relationResult = Relation.search(search);

		response.contentType = "text/csv";

		response.setHeader("Content-Disposition",
				"attachment;filename=Results.csv");

		render("QuickSearch/Results.csv", entityResult, tagResult, ideaResult, relationResult);
	}

}
